package com.gp.testonline.controller;

import com.gp.testonline.eo.StudentEOPage;
import com.gp.testonline.model.Student;
import com.gp.testonline.service.StudentService;
import com.gp.testonline.tool.ResponseMessage;
import com.gp.testonline.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "student")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "添加学生信息方法")
    @RequestMapping(value = "/insertStudent",method = RequestMethod.GET)
    public ResponseMessage insert(Student student){
        int IsInsert=0;
        //传入数据的判断
        //（1）实体对象不能为空
        if (student!=null){
            //(2) 学号不能为空
            if (student.getStudentid()!=null){
                //(3)添加角色数据
                IsInsert=studentService.InsertStudent(student);
                if (IsInsert==1){
                    return Result.success("添加成功！");
                }else if (IsInsert==-1){
                    return Result.error("学号重复不允许添加！");
                }else{
                    return Result.error("添加失败！");
                }
            }
        }
        return Result.error("添加失败！");
    }

    @ApiOperation(value = "学生信息修改方法")
    @RequestMapping(value = "/updateStudent",method = RequestMethod.GET)
    public ResponseMessage updateStudent(Student student){
        int isUpdate=0;
        //传入的参数不能为空
        if(student!=null){
            //参数当中主键不能为0
            if (student.getSid()!=0){
                isUpdate=studentService.UpdateStudent(student);
                if (isUpdate==1){
                    return Result.success("更新成功！");
                }else {
                    return Result.error("更新失败！");
                }
            }else {
                return Result.error("传入的参数不正确");
            }
        }
        return Result.error("传入的参数不正确");
    }

    @ApiOperation(value = "学生信息删除方法")
    @RequestMapping(value = "/deleteStudent",method = RequestMethod.GET)
    public ResponseMessage deleteStudent(int sid){
        int isDelete=0;
        if (sid!=0){
            isDelete=studentService.DeleteStudent(sid);
            if (isDelete>0){
                return Result.success("删除成功！");
            }else{
                return Result.error("删除失败！");
            }
        }else {
            return Result.error("删除的主键不能为0，请重新输入！");
        }
    }

    @ApiOperation(value = "根据条件获取学生详细信息")
    @RequestMapping(value = "/getStudent",produces = "application/json;charest=utf-8",method = RequestMethod.PUT)
    public ResponseMessage getStudent(StudentEOPage studentEOPage) {
        Map<String, List> map = new HashMap<>();
        List<StudentEOPage> listStudent = studentService.getStudentAll(studentEOPage);
        map.put("list",listStudent);
        return Result.success("200",map);
    }
}
